package com.yangxk.kadmin.web.module.system.controller;

import com.bstek.ureport.export.html.HtmlReport;
import com.yangxk.kadmin.common.beans.Result;
import com.yangxk.kadmin.web.module.system.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private ReportService reportService;

    @GetMapping("/detail")
    @ResponseBody
    public Result getReport(String filename) {
        return Result.ofSuccess(reportService.getReportContent(filename, null, new HashMap<>()));
    }

    @GetMapping("/html")
    public String getHtml(String filename, Model model, HttpServletRequest request,
                          @RequestParam(required = false) Integer num) {
        Map<String, Object> paramMap = new HashMap<>();
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String s = parameterNames.nextElement();
            String value = request.getParameter(s);
            paramMap.put(s, value);
        }
        HtmlReport report = reportService.getReportContent(filename, num, paramMap);
        model.addAttribute("report", report);

        return "report";
    }

}
